package cn.message.service.impl;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.mapper.packageOrderItemMapper;
import cn.itcast.mapper.packageOrderMapper;
import cn.itcast.pojo.*;
import cn.message.service.IPackageOrderService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 郭智超
 * @create 2020-05-27 8:35
 */
@Service
@Slf4j
public class PackageOrderServiceImpl implements IPackageOrderService {

    @Resource
    private packageOrderMapper packageOrderMapper;

    @Resource
    private packageOrderItemMapper packageOrderItemMapper;

    @Resource
    private horsemanMapper horsemanMapper;

    @Override
    public Map<String, Object> getAllPackageOrder(Map<String, String> map) {
//        //获取前台传递的数据
//        Integer page = Integer.parseInt(map.get("page"));
//        Integer limit = Integer.parseInt(map.get("limit"));
//        String orderNumber = map.get("orderNumber");
//        String horsemanName = map.get("horsemanName");
//        String status = map.get("status");
//        String adminPhone = map.get("adminPhone");
//        String role = map.get("role");
//        String[] dateRanges = {};
//        Date startDate = null;
//        Date endDate = null;
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        if (!map.get("dateRange").isEmpty()) {
//            dateRanges = map.get("dateRange").split(",");
//            try {
//                startDate = sdf.parse(dateRanges[0]);
//                endDate = sdf.parse(dateRanges[1]);
//
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//        }
//        log.debug(adminPhone);
//        HashMap<String, Object> parameterMap = new HashMap<String,Object>();
//        parameterMap.put("orderNumber",orderNumber);
//        parameterMap.put("horsemanName",horsemanName);
//        parameterMap.put("status",status);
//        parameterMap.put("adminPhone",adminPhone);
//        parameterMap.put("startDate",startDate);
//        parameterMap.put("endDate",endDate);
//        parameterMap.put("role",role);
//        int count = packageOrderMapper.getAllPackageOrder(parameterMap).size();
//        //分页
//        PageHelper.startPage(page, limit);
//        List<packageOrder> packageOrderList = packageOrderMapper.getAllPackageOrder(parameterMap);

        //获取前台传递的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        String orderNumber = map.get("orderNumber");
        String horsemanName = map.get("horsemanName");
        String status = map.get("status");
        String adminPhone = map.get("adminPhone");
        String role = map.get("role");
        String[] dateRanges = {};
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (!map.get("dateRange").isEmpty()) {
            dateRanges = map.get("dateRange").split(",");
            try {
                startDate = sdf.parse(dateRanges[0]);
                endDate = sdf.parse(dateRanges[1]);

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        //添加查询条件
        packageOrderExample packageOrderExample = new packageOrderExample();
        cn.itcast.pojo.packageOrderExample.Criteria criteria = packageOrderExample.createCriteria();
        if(!status.isEmpty()){
            criteria.andStatusEqualTo(status);
        }
        if(!orderNumber.isEmpty()){
            criteria.andOrderNumberLike("%"+orderNumber+"%");
        }
        if(!horsemanName.isEmpty()){
            criteria.andHorsemanNameLike("%"+horsemanName+"%");
        }
        if(!map.get("dateRange").isEmpty()){
            criteria.andPayTimeBetween(startDate,endDate);
        }

        log.debug("排序字段是："+map.get("prop"));
        log.debug("排序类型是："+map.get("order"));
        //按某个属性排序，默认按支付时间逆序排序
        if(map.get("prop") != null && map.get("prop") != "" && map.get("order") != null && map.get("order") != ""){
            String order = "desc";
            if(map.get("order").equals("ascending")){
                order = "asc";
            }
            if(map.get("prop").equals("payTime")){
                packageOrderExample.setOrderByClause("pay_time "+order);
            }
        }else {
            packageOrderExample.setOrderByClause("pay_time desc");
        }

        //判断登录的用户是否是商家管理员，如果是，则查询该商家旗下的骑手的订单，如果不是，则查询所有的订单
        if(!map.get("adminPhone").isEmpty() && role.contains("merchantAdmin")){
            horsemanExample horsemanExample = new horsemanExample();
            cn.itcast.pojo.horsemanExample.Criteria horsemanCriteria = horsemanExample.createCriteria();
            horsemanCriteria.andHorsemanReferrerEqualTo(map.get("adminPhone"));
            List<horseman> horsemanList = horsemanMapper.selectByExample(horsemanExample);
            ArrayList<String> horsemanNameList = new ArrayList<>();
            for (horseman horseman : horsemanList) {
                horsemanNameList.add(horseman.getHorsemanName());
            }
            criteria.andHorsemanNameIn(horsemanNameList);
        }
        int count = packageOrderMapper.selectByExample(packageOrderExample).size();
        //分页
        PageHelper.startPage(page, limit);
        List<packageOrder> packageOrderList = packageOrderMapper.selectByExample(packageOrderExample);

        //格式化打包时间和支付时间
        for (packageOrder packageOrder : packageOrderList) {
            if(packageOrder.getOrderTime() != null){
                packageOrder.setOrderTimeString(sdf.format(packageOrder.getOrderTime()));
            }
            if(packageOrder.getPayTime() != null){
                packageOrder.setPayTimeString(sdf.format(packageOrder.getPayTime()));
            }
        }

        for (packageOrder packageOrder : packageOrderList) {
            List<packageOrderItem> packageOrderItem = packageOrderItemMapper.getPackageOrderItem(packageOrder.getId());
            packageOrder.setPackageOrderItems(packageOrderItem);
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",count);
        resultMap.put("packageOrderList",packageOrderList);
        return resultMap;
    }

    @Override
    public Map<String, Object> getAllPackageOrderItem(Map<String, String> map) {
        //获取前台传递的数据
        Integer page = Integer.parseInt(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        int packageOrderId = Integer.parseInt(map.get("packageOrderId"));
        packageOrderItemExample packageOrderItemExample = new packageOrderItemExample();
        cn.itcast.pojo.packageOrderItemExample.Criteria criteria = packageOrderItemExample.createCriteria();
        criteria.andOrderIdEqualTo(packageOrderId);

        int count = packageOrderItemMapper.countByExample(packageOrderItemExample);
        PageHelper.startPage(page,limit);
        List<packageOrderItem> packageOrderItemList = packageOrderItemMapper.selectByExample(packageOrderItemExample);

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",count);
        resultMap.put("packageOrderItemList",packageOrderItemList);

        return resultMap;
    }

    @Override
    public List<packageOrder> exprotPackageOrder(Map<String, String> map) {
        String orderNumber = map.get("orderNumber");
        String horsemanName = map.get("horsemanName");
        String status = map.get("status");
        String adminPhone = map.get("adminPhone");
        String role = map.get("role");
        String[] dateRanges = {};
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        if (!map.get("dateRange").isEmpty()) {
            dateRanges = map.get("dateRange").split(",");
            try {
                startDate = sdf.parse(dateRanges[0]);
                endDate = sdf.parse(dateRanges[1]);

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        HashMap<String, Object> parameterMap = new HashMap<String,Object>();
        parameterMap.put("orderNumber",orderNumber);
        parameterMap.put("horsemanName",horsemanName);
        parameterMap.put("status",status);
        log.debug("状态："+status);
        parameterMap.put("adminPhone",adminPhone);
        parameterMap.put("startDate",startDate);
        parameterMap.put("endDate",endDate);
        parameterMap.put("role",role);
        log.debug("角色："+role);
        int count = packageOrderMapper.getAllPackageOrder(parameterMap).size();
        List<packageOrder> packageOrderList = packageOrderMapper.getAllPackageOrder(parameterMap);

        //格式化打包时间和支付时间
        for (packageOrder packageOrder : packageOrderList) {
            if(packageOrder.getOrderTime() != null){
                packageOrder.setOrderTimeString(sdf.format(packageOrder.getOrderTime()));
            }
            if(packageOrder.getPayTime() != null){
                packageOrder.setPayTimeString(sdf.format(packageOrder.getPayTime()));
            }
        }

        for (packageOrder packageOrder : packageOrderList) {
            List<packageOrderItem> packageOrderItem = packageOrderItemMapper.getPackageOrderItem(packageOrder.getId());
            packageOrder.setPackageOrderItems(packageOrderItem);
        }
        return packageOrderList;
    }


}
